package com.flame.user.service;

import com.flame.user.constants.MenuConstants;
import com.flame.user.controller.vo.MenuAddVo;
import com.flame.user.entity.Menu;
import com.flame.user.exception.UserExceptionMessage;
import com.flame.user.exception.UserRuntimeException;
import com.flame.user.mapper.MenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author wuxintong
 * @since 2022/10/26
 */
@Service
public class MenuService {

    @Autowired
    private MenuMapper menuMapper;

    public List<Menu> list() {
        return menuMapper.list();
    }

    public void addMenu(MenuAddVo menuAddVo) {
        Menu dbMenu = menuMapper.selectById(menuAddVo.getMenu().getMenuCode());
        if (dbMenu != null) {
            throw new UserRuntimeException(UserExceptionMessage.MENU_EXIST, menuAddVo.getMenu().getMenuCode());
        }
        menuMapper.insert(menuAddVo.getMenu());
        if (!CollectionUtils.isEmpty(menuAddVo.getBtnList())) {
            for (Menu btn : menuAddVo.getBtnList()) {
                Menu dbBtn = menuMapper.selectById(btn.getMenuCode());
                if (dbBtn == null) {
                    btn.setMenuStatus(MenuConstants.MENU_STATUS_SHOW);
                    btn.setMenuType(MenuConstants.MENU_TYPE_BTN);
                    btn.setLoadType(MenuConstants.LOAD_TYPE_VUE);
                    btn.setMenuParent(menuAddVo.getMenu().getMenuCode());
                    btn.setMenuSort(0);
                    menuMapper.insert(btn);
                }
            }
        }
    }

    public void updateMenu(MenuAddVo menuAddVo) {
        menuMapper.updateById(menuAddVo.getMenu());
        if (MenuConstants.MENU_TYPE_PAGE.equals(menuAddVo.getMenu().getMenuType())) {
            menuMapper.deleteByMenuParent(menuAddVo.getMenu().getMenuCode());
            if (!CollectionUtils.isEmpty(menuAddVo.getBtnList())) {
                for (Menu btn : menuAddVo.getBtnList()) {
                    Menu dbBtn = menuMapper.selectById(btn.getMenuCode());
                    if (dbBtn == null) {
                        btn.setMenuStatus(MenuConstants.MENU_STATUS_SHOW);
                        btn.setMenuType(MenuConstants.MENU_TYPE_BTN);
                        btn.setLoadType(MenuConstants.LOAD_TYPE_VUE);
                        btn.setMenuParent(menuAddVo.getMenu().getMenuCode());
                        btn.setMenuSort(0);
                        menuMapper.insert(btn);
                    }
                }
            }
        }
    }

    public void deleteMenu(String menuCode) {
        List<Menu> list = menuMapper.selectByMenuParent(menuCode);
        if (list != null && list.size() > 0) {
            for (Menu menu : list) {
                deleteMenu(menu.getMenuParent());
            }
        }
        menuMapper.deleteById(menuCode);
    }

    public List<Menu> listMenuBtn(String menuCode) {
        return menuMapper.listMenuBtn(menuCode);
    }

    public Menu getByMenuCode(String menuCode) {
        return menuMapper.selectById(menuCode);
    }
}
